package com.woniuxy.usercoachassemble.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.API.UserAPI;
import com.woniuxy.entityDTO.UserDTO;
import com.woniuxy.usercoachassemble.configuration.RabbitmqConfiger;
import com.woniuxy.usercoachassemble.entity.*;
import com.woniuxy.usercoachassemble.service.*;
import org.example.Result;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class RabbitmqController1 {
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CoachInfoService coachInfoService;
    @Autowired
    private YogaGymSignedCoachService yogaGymSignedCoachService;
    @Autowired
    private  InformController informController;

        /**
         * 监听我们的死信队列
         */
        @RabbitListener(queues = RabbitmqConfiger.ORDERS_SCAN_QUEUE)
        public void orderConsumer(Map<String,Object> map) throws IOException {

            System.out.println("接受到教练与学员的订单消息");

            int cocahId = (int)map.get("CocahId");
            int userId = (int)map.get("UserId");
            double money = (double)map.get("money");
            //员工
            QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId);
            Userinfo userinfo = userinfoService.getOne(wrapper);
            Integer userinfoAcccountId = userinfo.getAcccountId();
            //算钱
            Account userbyId = accountService.getById(userinfoAcccountId);
            Double money1 = userbyId.getMoney();
            userbyId.setMoney(money1-money);
            accountService.updateById(userbyId);


            //教练
            QueryWrapper<Userinfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("user_id",cocahId);
            Userinfo cocahinfo = userinfoService.getOne(wrapper1);
            Integer cocahAcccountId = cocahinfo.getAcccountId();
            Account cocahbyId = accountService.getById(cocahAcccountId);
            Double money2 = cocahbyId.getMoney();
            cocahbyId.setMoney(money2+(money*0.8));
            accountService.save(cocahbyId);
            //查看教练信息
            QueryWrapper<CoachInfo> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("user_id", cocahId);
            CoachInfo one = coachInfoService.getOne(wrapper2);
            Integer indentification = one.getIndentification();
            if(indentification==2){
                //平台
                Admin byId = adminService.getById(1);
                double money3 = byId.getMoney();
                byId.setMoney(money3+money1*0.1);
                adminService.updateById(byId);

                //会馆
                QueryWrapper<YogaGymSignedCoach> wrapper3 = new QueryWrapper<>();
                wrapper3.eq("coach_id",cocahId);
                wrapper3.eq("status","2");
                YogaGymSignedCoach one1 = yogaGymSignedCoachService.getOne(wrapper3);
                Integer yogaGymId = one1.getYogaGymId();
                QueryWrapper<Userinfo> wrapper4 = new QueryWrapper<>();
                wrapper.eq("user_id",yogaGymId);
                Userinfo one2 = userinfoService.getOne(wrapper4);
                Integer acccountId = one2.getAcccountId();

                //算钱
                Account account = accountService.getById(acccountId);
                Double money4 = account.getMoney();
                userbyId.setMoney(money4+money*0.1);
                accountService.updateById(account);


            }else{
                //平台
                Admin byId = adminService.getById(1);
                double money3 = byId.getMoney();
                byId.setMoney(money3+money1*0.2);
                adminService.updateById(byId);
            }

            informController.insertInform(userId,cocahId,"付款成功",1);
            informController.insertInform(cocahId,userId,"收到一笔资金",1);

        }


}
